home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / thermometer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-28  |  6.3 KB  |  247 lines

  1. /*
  2.     Thermometer XCMD v1.0
  3.     
  4.     ©1991 Apple Computer, Inc.; by Mike Byrne
  5.     
  6.     This XCMD can be used to make little "Thermometer"-like progress bars.  It should be passed the
  7.     rect in which the bar should be painted, the current value of some counter, the maximum value
  8.     of that counter, and, optionally, a color.  The colors are the basic quickDraw colors, and the
  9.     default is black.  It will check to see if the rect is longer or tall.  If it is long, it fills
  10.     left to right.  If it is tall, it fills top to bottom.
  11.     
  12.     Form:
  13.     Thermometer <rect>, <current>, <maximum>, [<color>]
  14.     
  15.     # the MPW 3.2 build commands:
  16.     C -b Thermometer.c -mbg off
  17.         Link -w -t STAK -c WILD -rt XCMD=617 ∂
  18.             -m ENTRYPOINT ∂
  19.             -sg Thermometer ∂
  20.             Thermometer.c.o ∂
  21.             "{Libraries}HyperXLib.o" ∂
  22.             "{Libraries}Runtime.o" ∂
  23.             "{Libraries}Interface.o" ∂
  24.             "{CLibraries}StdCLib.o" ∂
  25.             -o "teststack"
  26. */
  27.  
  28. #include <Types.h>
  29. #include <Windows.h>
  30. #include <string.h>
  31. #include <Memory.h>
  32. #include <QuickDraw.h>
  33. #include "HyperXCmd.h"
  34.  
  35. #define NULL '\0'
  36. #define NIL 0L
  37. #define FALSE 0
  38. #define TRUE 1
  39.  
  40. #define kNumParams 3
  41.  
  42.  
  43. /* prototypes */
  44. void ErrorBack(XCmdPtr paramPtr, char *message);
  45. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  46. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  47. short SwitchColor( char* ask );
  48. Boolean StringRect(char* theString, Rect* theRect);
  49.  
  50.  
  51. pascal void EntryPoint(XCmdPtr paramPtr)
  52. {
  53.     /* variable declarations */
  54.     char    rectStr[20];
  55.     char    curStr[10];
  56.     char    maxStr[10];
  57.     long    curVal;
  58.     long    maxVal;
  59.     Rect    theRect;
  60.     short    theColor;
  61.     short    width;
  62.     short    height;
  63.     WindowPtr    theWindow;
  64.     
  65.     
  66.     /* move high and lock the parameters. */
  67.     MoveLockParams(paramPtr, paramPtr->paramCount);
  68.  
  69.     /* check for copyright or syntax help request */
  70.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  71.         ErrorBack(paramPtr, "v1.0, ©1991 Apple Computer, Inc.; by Mike Byrne");
  72.         UnlockParams(paramPtr, paramPtr->paramCount);
  73.         return;
  74.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  75.         ErrorBack(paramPtr, "Thermometer syntax is 'Thermometer <rect>, <current>, <maximum>, [<color>], [<pattern>]'");
  76.         UnlockParams(paramPtr, paramPtr->paramCount);
  77.         return;
  78.     }
  79.  
  80.     /* not a copyright or help request.       */     
  81.     /* check for correct number of parameters */
  82.     if (paramPtr->paramCount < kNumParams) {
  83.         ErrorBack(paramPtr, "Error: Thermometer syntax is 'Thermometer <rect>, <current>, <maximum>, [<color>], [<pattern>]'");
  84.         UnlockParams(paramPtr, paramPtr->paramCount);
  85.         return;
  86.     }
  87.  
  88.     GetPort(&theWindow);
  89.     SetPort(theWindow);
  90.     
  91.     /* set up the basic parameters and such. */
  92.     strcpy(rectStr, (char*)*paramPtr->params[0]);
  93.     strcpy(curStr, (char*)*paramPtr->params[1]);
  94.     strcpy(maxStr, (char*)*paramPtr->params[2]);
  95.     c2pstr(curStr);
  96.     c2pstr(maxStr);
  97.     if (!StringRect(rectStr, &theRect)) {
  98.         ErrorBack(paramPtr, "Error: One of the 'rect' values out-of-range.");
  99.         UnlockParams(paramPtr, paramPtr->paramCount);
  100.         return;
  101.     }
  102.     StringToNum(curStr, &curVal);
  103.     StringToNum(maxStr, &maxVal);
  104.     if (curVal < 0) { curVal = 0; }
  105.     if (maxVal < 1) { maxVal = 1; }
  106.     if (curVal > maxVal) { curVal = maxVal; }
  107.     
  108.     /* set the color */
  109.     if ( (paramPtr->paramCount < 4) || !(theColor = SwitchColor((char*)*paramPtr->params[3])) ) {
  110.         theColor = blackColor;
  111.     }
  112.     ForeColor(theColor);
  113.     
  114.     /* specify the rect */
  115.     InsetRect(&theRect,1,1);
  116.     width = theRect.right - theRect.left;
  117.     height = theRect.bottom - theRect.top;
  118.     if (width >= height) {
  119.         theRect.right = theRect.left + (width*curVal/maxVal);
  120.     } else {
  121.         theRect.top = theRect.bottom - (height*curVal/maxVal);
  122.     }
  123.     PaintRect(&theRect);
  124.     ForeColor(blackColor);
  125. }
  126.  
  127.  
  128.  
  129.  
  130. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++
  131.     SwitchColor takes a string and returns a color... */
  132. short SwitchColor( char* ask )
  133. {
  134.     short     i;
  135.     
  136.     /* convert the string to uppercase */
  137.     for (i=0; i < strlen(ask); i++) {
  138.         ask[i] = toupper(ask[i]);
  139.     }
  140.     
  141.     /* switch on the string. */
  142.     if (!strcmp(ask, "BLACK") || !strcmp(ask, "1")) 
  143.         return(blackColor);
  144.     else if (!strcmp(ask, "WHITE") || !strcmp(ask, "2")) 
  145.         return(whiteColor);
  146.     else if (!strcmp(ask, "RED")  || !strcmp(ask, "3")) 
  147.         return(redColor);
  148.     else if (!strcmp(ask, "GREEN") || !strcmp(ask, "4")) 
  149.         return(greenColor);
  150.     else if (!strcmp(ask, "BLUE") || !strcmp(ask, "5")) 
  151.         return(blueColor);
  152.     else if (!strcmp(ask, "CYAN") || !strcmp(ask, "6")) 
  153.         return(cyanColor);
  154.     else if (!strcmp(ask, "MAGENTA") || !strcmp(ask, "7")) 
  155.         return(magentaColor);
  156.     else if (!strcmp(ask, "YELLOW") || !strcmp(ask, "8")) 
  157.         return(yellowColor);
  158.     else return(0);
  159. }
  160.  
  161.  
  162. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  163.     StringRect converts a string of the form "l,t,b,r" to a rect.   */
  164. Boolean StringRect(char* theString, Rect* theRect)
  165. {
  166.     char    leftStr[10];
  167.     char    topStr[10];
  168.     char    rightStr[10];
  169.     char    bottomStr[10];
  170.     long    left;
  171.     long    top;
  172.     long    right;
  173.     long     bottom;
  174.     
  175.     strcpy(leftStr, strtok(theString,","));
  176.     c2pstr(leftStr);
  177.     StringToNum(leftStr, &left);
  178.     if (left < 0) { return(FALSE); }
  179.     
  180.     strcpy(topStr, strtok(NULL,","));
  181.     c2pstr(topStr);
  182.     StringToNum(topStr, &top);
  183.     if (top < 0) { return(FALSE); }
  184.     
  185.     strcpy(rightStr, strtok(NULL,","));
  186.     c2pstr(rightStr);
  187.     StringToNum(rightStr, &right);
  188.     if (right < 0) { return(FALSE); }
  189.     
  190.     strcpy(bottomStr, strtok(NULL,","));
  191.     c2pstr(bottomStr);
  192.     StringToNum(bottomStr, &bottom);
  193.     if (bottom < 0) { return(FALSE); }
  194.  
  195.     theRect->left = left;
  196.     theRect->top = top;
  197.     theRect->right = right;
  198.     theRect->bottom = bottom;
  199.     return(TRUE);
  200. }
  201.     
  202.     
  203.     
  204. /* allocate and load up paramPtr->returnValue with a string 
  205.    -------------------------------------------------------- */
  206. void ErrorBack(XCmdPtr paramPtr, char *message)
  207. {
  208.     Handle  mesHnd;
  209.  
  210.     /*
  211.         Allocate space for an error message.
  212.         Copy the string into it.
  213.         Return the handle to HyperCard.
  214.     */
  215.     mesHnd = NewHandle((long)(strlen(message)+1));
  216.     if (mesHnd == NIL) return;
  217.     strcpy((char *)*mesHnd,message);
  218.     paramPtr->returnValue = mesHnd;
  219. }
  220.  
  221.  
  222.  
  223. /*  move high and lock down all parameters  
  224.     ----------------------------------------------------------------------- */
  225. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  226. {
  227.     short i;
  228.     
  229.     for(i=0; i <= paramCount-1; i++)
  230.     {
  231.         MoveHHi(paramPtr->params[i]);
  232.         HLock(paramPtr->params[i]);
  233.     }
  234. }
  235.  
  236.  
  237.  
  238.  
  239. /* unlock all parameter handles in the XCmdBlock  
  240.    ---------------------------------------------  */
  241. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  242. {    short i;
  243.     
  244.     for(i=0; i <= paramCount-1; i++)
  245.         { HUnlock(paramPtr->params[i]);}
  246. }
  247.